﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class back_PaymentDetails : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    PaymentHistory pHistory;
    User user;
    List<PaymentHistory> pHisotries = new List<PaymentHistory>();
    List<User> users = new List<User>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindArchitects();
            bindUsers();
        }
    }
    private void bindUsers()
    {
        user = new User(_dataObject);
        users = user.GetAllUsers();

        if (users.Count > 0)
        {
            drpUser.DataSource = users;
            drpUser.DataValueField = "Id";
            drpUser.DataTextField = "FirstName";
            drpUser.DataBind();
        }
    }
    private void bindArchitects()
    {
        pHistory = new PaymentHistory(_dataObject);
        pHisotries = pHistory.GetAllPayments();

        if (pHisotries.Count > 0)
        {
            grdPayment.Visible = true;
            grdPayment.DataSource = pHisotries;
            grdPayment.DataBind();
        }
        else
        {
            grdPayment.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdPayment_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdPayment.PageIndex = e.NewPageIndex;
        bindArchitects();
    }
    protected void grdPayment_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnUserId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnUserId");
            Label lblName = (Label)e.Row.FindControl("lblName");
            Label lblAmount = (Label)e.Row.FindControl("lblAmount");
            Label lblDate = (Label)e.Row.FindControl("lblDate");

            user = new CDB.LinqModel.User(_dataObject);
            user.Id = Convert.ToInt32(hdnUserId.Text);
            user.GetUserById();

            if (user.Id != 0)
                lblName.Text = user.FirstName + " " + user.LastName;

            lblDate.Text = Convert.ToDateTime(lblDate.Text).ToShortDateString();
            lblAmount.Text = Convert.ToDecimal(lblAmount.Text).ToString("C");
        }
    }
}